package com.example.mygame.controller;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.mygame.R;
import com.example.mygame.service.Adopter;
import com.example.mygame.pojo.Friend;
import com.example.mygame.pojo.User;
import com.example.mygame.service.SqliteUtils;

import java.util.List;

public class AddFriend extends AppCompatActivity {

    private SqliteUtils sqliteUtils;

    private ListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);

        listView = findViewById(R.id.list);

        sqliteUtils = new SqliteUtils(this);


        Intent getDataIntent = getIntent();

        String userName = getDataIntent.getStringExtra("userName");

        List<User> userList = sqliteUtils.queryAllFromDb();

        ListView listView = (ListView) findViewById(R.id.list);

        listView.setAdapter(new Adopter(userList,this));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView name = view.findViewById(R.id.name);
                String Fname = name.getText().toString();

                Friend friend = new Friend();

                friend.setUsername(userName);

                friend.setFriendname(Fname);

                friend.setStatus(0);

                sqliteUtils.addFriend(friend);


            }
        });

    }
}